package com.koudai.reader.conponment;

import com.koudai.reader.util.HttpUtils;
import com.koudai.reader.vo.EndUser;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.text.Html;

public class ExistDialog {

    public static void showDialog(final Activity context) {

        SharedPreferences config = context.getSharedPreferences("config", 0);
        boolean isRate = config.getBoolean("isRate", false);

        if (!isRate) {
            new AlertDialog.Builder(context).setTitle("退出").setMessage(Html.fromHtml("谢谢您的支持，如觉得书海小二不错,请给我评分吧，^_^<br/><br/><font color='red'>评论即送200积分<font/>!"))
                    .setNegativeButton("下次再评", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            context.finish();
                        }
                    }).setPositiveButton("去评价", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            Intent intent = new Intent(Intent.ACTION_VIEW);  
                            intent.setData(Uri.parse("market://details?id=" + context.getPackageName()));  
                            context.startActivity(intent);  
                            SharedPreferences config = context.getSharedPreferences("config", 0);
                            config.edit().putBoolean("isRate", true).commit();
                            new Thread(new Runnable() {
                                
                                @Override
                                public void run() {
                                    HttpUtils.addGold4user(200);
                                    EndUser.getInstance().updateGold(200);
                                }
                            }).start();
                        }
                    }).setNeutralButton("点错了", new OnClickListener() {
                        
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // TODO Auto-generated method stub
                            
                        }
                    }).show();
        } else {
            new AlertDialog.Builder(context).setTitle("退出").setMessage("确定要退出吗").setNegativeButton("点错了", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                }
            }).setPositiveButton("下次再来", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    context.finish();
                }
            }).show();
        }

    }

}
